package unit16.transform;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.AttributedCharacterIterator.Attribute;
import java.util.StringTokenizer;

import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.AttributesImpl;

public class EmployeeReader implements XMLReader {
	private ContentHandler handler;
	@Override
	public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
		// TODO Auto-generated method stub

	}

	@Override
	public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setEntityResolver(EntityResolver resolver) {
		// TODO Auto-generated method stub

	}

	@Override
	public EntityResolver getEntityResolver() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setDTDHandler(DTDHandler handler) {
		// TODO Auto-generated method stub

	}

	@Override
	public DTDHandler getDTDHandler() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setContentHandler(ContentHandler handler) {
		// TODO Auto-generated method stub
		this.handler=handler;
	}

	@Override
	public ContentHandler getContentHandler() {
		// TODO Auto-generated method stub
		return handler;
	}

	@Override
	public void setErrorHandler(ErrorHandler handler) {
		// TODO Auto-generated method stub

	}

	@Override
	public ErrorHandler getErrorHandler() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void parse(InputSource input) throws IOException, SAXException {
		// TODO Auto-generated method stub
		InputStream stream=input.getByteStream();
		BufferedReader in=new BufferedReader(new InputStreamReader(stream));
		String rootElement="staff";
		AttributesImpl attrs=new AttributesImpl();
		
		if(handler==null){
			throw new SAXException("No content handler");
		}
		handler.startDocument();
		handler.startElement("", rootElement, rootElement, attrs);
		String line;
		while((line=in.readLine())!=null){
			handler.startElement("", "employee", "employee", attrs);
			StringTokenizer t=new StringTokenizer(line,"|");
			 
			handler.startElement("", "name", "name", attrs);
			String s=t.nextToken();
			handler.characters(s.toCharArray(), 0, s.length());
			handler.endElement("", "name", "name");
			
			handler.startElement("", "salary", "salary", attrs);
			s=t.nextToken();
			handler.characters(s.toCharArray(), 0, s.length());
			handler.endElement("", "salary", "salary");
			
			attrs.addAttribute("", "year", "year", "CDATA", t.nextToken());
			attrs.addAttribute("", "month", "month", "CDATA", t.nextToken());
			attrs.addAttribute("", "day", "day", "CDATA", t.nextToken());
			handler.startElement("", "date", "date", attrs);
			handler.endElement("", "date", "date");
			attrs.clear();
			
			handler.endElement("", "employee", "employee");
		}
		handler.endElement("", rootElement, rootElement);
		handler.endDocument();
		
	}

	@Override
	public void parse(String systemId) throws IOException, SAXException {
		// TODO Auto-generated method stub
		
	}

}
